package com.ibuonline.b2b.admin.dao;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.dimeng.common.base.SuperDAO;
import com.ibuonline.b2b.entity.CmsHomeCategory;

public class CmsHomeCategoryDAO extends SuperDAO<CmsHomeCategory>{
	
	@SuppressWarnings("unchecked")
	public Boolean updateCatDisplayOrder(final String[] catId) {
		return (Boolean) template.execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Transaction transaction = null;
				try {
					transaction = session.beginTransaction();
					for(int i=0; i<catId.length; i++) {
						CmsHomeCategory chc = (CmsHomeCategory)session.get(CmsHomeCategory.class, Integer.parseInt(catId[i]));
						chc.setDisplayOrder(i);
						session.update(chc);
					}
					transaction.commit();
					return true;
				} catch (Exception e) {
					if (transaction != null)
						transaction.rollback();
					return false;
				}
			}
		});
	}
}
